package com.huike.clues.service.impl;

import com.huike.clues.domain.Activity;
import com.huike.clues.domain.dto.ActivityDTO;
import com.huike.mapper.ActivityMapper;
import com.huike.clues.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private  ActivityMapper activityMapper;
    /**
     * 查询活动信息列表
     *
     * @param activityDTO
     * @return
     */
    @Override
    public List<Activity> pageQuery(ActivityDTO activityDTO) {
        List<Activity> list = activityMapper.selectActivity(activityDTO);
        return list;
    }

    /**
     * 添加活动
     *
     * @param activity
     */
    @Override
    public int addActivity(Activity activity) {

        String code = generateShortUuid();
        activity.setCode(code);
        int row = activityMapper.addActivity(activity);
        return row;
    }

    /**
     * 根据渠道来源查询活动信息
     *
     * @param channel
     * @return
     */
    @Override
    public List<Activity> selectByChannel(Long channel) {
        List<Activity> activityList = activityMapper.selectByChannel(channel);
        return activityList;
    }

    /**
     * 根据id查询活动信息
     *
     * @param id
     * @return
     */
    @Override
    public Activity selectById(Long id) {
        Activity activity = activityMapper.selectById(id);
        return activity;
    }

    /**
     * 更新活动信息
     *
     * @param activity
     * @return
     */
    @Override
    public int updateActivity(Activity activity) {
        int rows = activityMapper.updateById();
        return rows;
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public int batchDelete(List ids) {
        int rows = activityMapper.batchDelete(ids);
        return rows;
    }

    //生成随机code
    public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z" };
    public static String generateShortUuid() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();

    }
}
